|
In programming languages, closures (also lexical closures or function closures) are a technique for implementing lexically scoped name binding in languages with first-class functions. Operationally, a closure is a record storing a function together with an environment:〔Sussman and Steele. "Scheme: An interpreter for extended lambda calculus". "... a data structure containing a lambda expression, and an environment to be used when that lambda expression is applied to arguments." (Wikisource)〕 a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or storage location to which the name was bound when the closure was created. A closure—unlike a plain function—allows the function to access those ''captured variables'' through the closure's reference to them, even when the function is invoked outside their scope. Example. The following program fragment defines a higher-order function startAt with a parameter x and a nested function incrementBy. The nested function incrementBy has access to x, because incrementBy is in the lexical scope of x, even though x is not local to incrementBy. The function startAt returns a closure containing the function incrementBy, which adds the y value to the x value, and a reference to the variable x ''from this invocation'' of startAt, so incrementBy will know where to find it once invoked: function startAt(x) function incrementBy(y) return x + y return incrementBy variable closure1 = startAt(1) variable closure2 = startAt(5) Note that, as startAt returns a function, the variables closure1 and closure2 are of function type. Invoking closure1(3) will return 4, while invoking closure2(3) will return 8. While closure1 and closure2 refer to the same function incrementBy, the associated environments differ, and invoking the closures will bind the name x to two distinct variables with different values in the two invocations, thus evaluating the function to different results. == History and etymology == The concept of closures was developed in the 1960s for the mechanical evaluation of expressions in the λ-calculus and was first fully implemented in 1970 as a language feature in the PAL programming language to support lexically scoped first-class functions.〔David A. Turner (2012). ("Some History of Functional Programming Languages" ). Trends in Functional Programming '12.〕 Peter J. Landin defined the term ''closure'' in 1964 as having an ''environment part'' and a ''control part'' as used by his SECD machine for evaluating expressions. Joel Moses credits Landin with introducing the term ''closure'' to refer to a lambda expression whose open bindings (free variables) have been closed by (or bound in) the lexical environment, resulting in a ''closed expression'', or closure. This usage was subsequently adopted by Sussman and Steele when they defined Scheme in 1975, a lexically scoped variant of LISP, and became widespread. 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Closure (computer programming)」の詳細全文を読む スポンサード リンク
|